<?php

/**
 * payment actions.
 *
 * @package    academia_de_escritores
 * @subpackage payment
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class paymentActions extends sfAeAction
{
  private function getDbUser(){
  	return $this->getUser()->getGuardUser()->getUser();
  }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
  public function executeSelectType(sfWebRequest $request){
      $Invoice=InvoiceTable::getInstance()->findOneById($request->getParameter("invoice_id"));
      $this->redirect("register_payment_invoice_depositBank",array("invoice_id"  => $Invoice->getId()));
  }
  public function executeBankDeposit(sfWebRequest $request){
      $Invoice=InvoiceTable::getInstance()->findOneById($request->getParameter("invoice_id"));
      $InvoicePayment=new InvoicePayment($Invoice);
      $InvoicePayment->setUserRegister($this->getDbUser());
      $InvoicePayment->setInvoice($Invoice);
      $this->form=new PaymentBankDepositForm($InvoicePayment);
  }
  
  public function executeCreateBankDeposit(sfWebRequest $r){
      $Invoice=InvoiceTable::getInstance()->findOneById($r->getParameter("invoice_id"));
      $InvoicePayment=new InvoicePayment();
      $InvoicePayment->setInvoice($Invoice);
      $InvoicePayment->setUserRegister($this->getDbUser());
      $this->form=new PaymentBankDepositForm($InvoicePayment);
      if($Payment=$this->processForm($r, $this->form)){
          $this->getUser()->setFlash("payment_id", $Payment->getId());
          $this->redirect("@register_payment_success");
      }else{
          $this->setTemplate("bankDeposit");
      }
  }
  
  public function executePaymentSuccess(sfWebRequest $r){
  	  $this->redirectUnless($this->getUser()->getFlash("payment_id",false), "@home");
  	  $this->html=SectionTable::getInstance()->findOneByCode(SectionTable::SECTION_RESTRATION_TRANSFER_OR_DEPOSIT_SUCCESS)->getInfo();
  	  
  }
  
  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $obj = $form->save();
      return $obj; 
    }
    return false;
  }
  
}
